---
title: Replication File
subtitle: The participatory implications of radicalized policy feedbacks
author: Sergio Garcia-Rios, Nazita Lajevardi, Kassra Oskooii, Hannah L Walker
output:
  html_document:
    theme: readable
    toc: yes
  pdf_document:
    toc: yes
  word_document:
    toc: yes
editor_options:
  chunk_output_type: console
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = T, warning = F, message = F)
```


***

# Description

The following file contains step-by-step instructions, including code, required to replicate all tables and figures presented in, ``The Participatory Implications of Racialized Policy Feedbacks.''

There are three files associated with this project: 

- The abbreviated, clean datafile used in the analysis, `cmps2016_pop.csv`,  

- The R markdown (.Rmd) file that includes all code developed to perform the analysis, and 

- The .html produced by the .Rmd file.'

`RMarkdown` is an R package, but familiarity with `RMarkdown` is not necessary to execute the code contained in the .Rmd file. 

# Code and relevant output



## Loading Libraries


```{r}
library(tidyverse)
library(corrplot)
library(haven)
library(margins)
library(stargazer)
library(ggthemes)
library(broom)
library(magrittr)
library(xtable)
library(psych)
library(pander)
```





## Loading recoded CMPS 2016 in .csv format

```{r}
df <- read.csv("../data/cmps2016_pop.csv") # Make sure to change directory accordingly
```




## Prepping the data for the loops to follow

### Fold data by contact
  
```{r}

df_short <-
  df %>%
  select(
    respid,
    voted, polpart_index,
    ContactIndex,
    Police, Courts, # contact
    Probation, Bail, # contact
    Halfway, Housing, Jail, # contact
    ChildWelfare, FamilyCourt, # contact
    PoliceCar, PoliceTreat, # new contact
    PoliceFoot, Arrested,
    police_alt, fine_alt, probation_alt, convict_alt, jail_alt, # new contact
    linkedfate, disc2, # Key IV
    race, lat_id, as_id, pan_id, # Key IV
    external_efficacy, pol_interest, worship_att,
    partyid, female, age18_29, age30_39, age40_64, citizen,
    education, inc_less40k, inc_40_79k, white, black, latino, asian,
    campaign_vol, donate, campaign_button, contact_rep, contact_any,
    work_others, meeting, protest, petition, boycott
  )


df_contact <-
  df_short %>%
  select(
    respid, Police, Courts,
    Probation, Bail,
    Halfway, Housing, Jail,
    ChildWelfare, FamilyCourt,
    ContactIndex, police_alt, fine_alt, probation_alt, convict_alt, jail_alt
  )


df_tidy <-
  df_short %>% gather(
    type, contact,
    -voted, -polpart_index, -respid,
    Police, Courts, # contact
    Probation, Bail, # contact
    Halfway, Housing, Jail, # contact
    ChildWelfare, FamilyCourt, # contact
    ContactIndex, police_alt, fine_alt, probation_alt, convict_alt, jail_alt,
    -linkedfate, -disc2, # Key IV
    -lat_id, -as_id, -pan_id, # Key IV
    -external_efficacy,
    -pol_interest, -worship_att, -
      partyid, -female, -age18_29, -age30_39,
    -age40_64, -citizen, -
      education, -inc_less40k, -inc_40_79k, -race, -white, -black, -latino, -asian,
    -campaign_vol, -donate, -campaign_button, -contact_rep, -contact_any,
    -work_others, -meeting, -protest, -petition, -boycott
  )



df_tidy_full <- left_join(df_contact, df_tidy, by = "respid")
```


## Descritive Tables

Note: Analysis proceeds chronological to how it's presented in the paper. Prefix A indicates figure or table appears in the appendix

### Table A1: Summary table of all variables

```{r, results='asis'}

desc_vars <- c(
  "polpart_index", "Police", "Courts", "Probation", "Bail", "Halfway", "Housing", "Jail", "ChildWelfare", "FamilyCourt", "linkedfate", "disc2", "external_efficacy", "pol_interest", "worship_att",
  "partyid", "female", "age18_29", "age30_39", "age40_64",
  "education", "inc_less40k", "inc_40_79k", "black", "latino", "asian"
)

stargazer(
  df %>% select(desc_vars) %>%
    data.frame(),
  summary.stat = c("min", "p25", "mean", "p75", "max", "sd"),
  covariate.labels = c(
    "Participation Index",
    "Police", "Courts", "Probation", "Bail",
    "Halfway", "Housing", "Jail", "Child Welfare", "Family Court",
    "Linked Fate", "Discrimination", "Political Efficacy",
    "Political Interest", "Worship Attendace",
    "Party ID", "Female", "Age: 18-29", "Age: 30-39", "Age: 40-64", "Education",
    "Income: <40k", "Income: 40-79k", "Black", "Latino", "Asian"
  ),
  out = "publication_tables2/descriptive.tex",
  title = "Descriptive statistics of variables used in the CMPS",
  label = "tab:descriptive",
  type = "html"
)
```


### Table 1: Distribution of institutional contact 
```{r }

Police <- round((prop.table(table(df_tidy_full$Police)) * 100), digits = 2)

Courts <- round((prop.table(table(df_tidy_full$Courts)) * 100), digits = 2)

Probation <- round((prop.table(table(df_tidy_full$Probation)) * 100), digits = 2)
Bail <- round((prop.table(table(df_tidy_full$Bail)) * 100), digits = 2)
Halfway <- round((prop.table(table(df_tidy_full$Halfway)) * 100), digits = 2)
Housing <- round((prop.table(table(df_tidy_full$Housing)) * 100), digits = 2)
Jail <- round((prop.table(table(df_tidy_full$Jail)) * 100), digits = 2)
ChildWelfare <- round((prop.table(table(df_tidy_full$ChildWelfare)) * 100), digits = 2)
Family <- round((prop.table(table(df_tidy_full$FamilyCourt)) * 100), digits = 2)

percents <- rbind(Police, Courts, Probation, Bail, Halfway, Housing, Jail, ChildWelfare, Family)

xt <- xtable(
  x = percents,
  align = "lcccc",
  type = "html",
  caption = "The distribution of contact with authoritarianc institutions in the CMPS",
  label = "tab:contact_dist"
)

addtorow <- list()
addtorow$pos <- list(nrow(xt))

print(xt,
  format.args = list(big.mark = ","),
  caption.placement = "top",
  file = "publication_tables2/dist_table.tex",
  hline.after = c(-1, 0, 9)
)
```



### Figure A1: Correlation matrix plot

```{r}
df_corr <-
  df_short %>%
  select(
    Police, Courts,
    Probation, Bail,
    Halfway, Housing, Jail,
    ChildWelfare, FamilyCourt
  )

res <- cor(as.matrix(df_corr))


col3 <- colorRampPalette(c("#af8dc3", "#f7f7f7", "#7fbf7b"))

corrplot(res,
  type = "upper",
  order = "AOE",
  method = c("number"),
  diag = FALSE,

  col = col3(100),
  tl.col = "black"
)
```


### Figure A2: Cross tabulation of police contact against all other institutions  under study

```{r}

welfare_pt_df <-
  df_short %>%
  group_by(ChildWelfare) %>%
  count(Police) %>%
  rename(Var = ChildWelfare) %>%
  mutate(
    Pct = prop.table(n) * 100,
    Group = "Child Welfare"
  )

family_pt_df <-
  df_short %>%
  group_by(FamilyCourt) %>%
  count(Police) %>%
  rename(Var = FamilyCourt) %>%
  mutate(
    Pct = prop.table(n) * 100,
    Group = "Family Court"
  )


court_pt_df <-
  df_short %>%
  group_by(Courts) %>%
  count(Police) %>%
  rename(Var = Courts) %>%
  mutate(
    Pct = prop.table(n) * 100,
    Group = "Courts"
  )


probation_pt_df <-
  df_short %>%
  group_by(Probation) %>%
  count(Police) %>%
  rename(Var = Probation) %>%
  mutate(
    Pct = prop.table(n) * 100,
    Group = "Probation"
  )

bail_pt_df <-
  df_short %>%
  group_by(Bail) %>%
  count(Police) %>%
  rename(Var = Bail) %>%
  mutate(
    Pct = prop.table(n) * 100,
    Group = "Bail"
  )

halfway_pt_df <-
  df_short %>%
  group_by(Halfway) %>%
  count(Police) %>%
  rename(Var = Halfway) %>%
  mutate(
    Pct = prop.table(n) * 100,
    Group = "Halfway"
  )

housing_pt_df <-
  df_short %>%
  group_by(Housing) %>%
  count(Police) %>%
  rename(Var = Housing) %>%
  mutate(
    Pct = prop.table(n) * 100,
    Group = "Housing"
  )

jail_pt_df <-
  df_short %>%
  group_by(Jail) %>%
  count(Police) %>%
  rename(Var = Jail) %>%
  mutate(
    Pct = prop.table(n) * 100,
    Group = "Jail"
  )



cont_tables <- rbind(
  welfare_pt_df,
  jail_pt_df,
  housing_pt_df,
  family_pt_df,
  court_pt_df,
  probation_pt_df,
  halfway_pt_df,
  bail_pt_df
)


ggplot(
  cont_tables,
  aes(
    x = factor(Var),
    y = Pct,
    fill = factor(Police) %>%
      fct_rev(),
    label = paste0(round(Pct), "%")
  )
) +
  geom_col(alpha = .75) +
  geom_text(
    position = position_stack(vjust = 0.5),
    size = 2.3
  ) +
  # scale_fill_brewer(palette = "GnBu", direction = -1) +
  scale_fill_tableau(
    palette = "Tableau 10",
    labels = c(
      "Very often", "Sometimes",
      "Occasionally", "Never"
    )
  ) +
  scale_x_discrete(labels = c(
    "Never", "Occasionally",
    "Sometimes", "Very often"
  )) +
  coord_flip() +
  facet_wrap(~Group, ncol = 2) +
  theme_minimal() +
  labs(
    x = "",
    fill = "Level of Poice Contact",
    y = "% Distribution"
  ) +
  theme(legend.position = "bottom") +
  ggsave("publication_plots2/xtabs_police.pdf",
    width = 8, height = 6
  )
```



### Figure A3: Cross tabulation of child welfare contact against all other institutions under study

```{r}

police_wf_df <-
  df_short %>%
  group_by(Police) %>%
  count(ChildWelfare) %>%
  rename(Var = Police) %>%
  mutate(
    Pct = prop.table(n) * 100,
    Group = "Police"
  )

family_wf_df <-
  df_short %>%
  group_by(FamilyCourt) %>%
  count(ChildWelfare) %>%
  rename(Var = FamilyCourt) %>%
  mutate(
    Pct = prop.table(n) * 100,
    Group = "Family Court"
  )


court_wf_df <-
  df_short %>%
  group_by(Courts) %>%
  count(ChildWelfare) %>%
  rename(Var = Courts) %>%
  mutate(
    Pct = prop.table(n) * 100,
    Group = "Courts"
  )


probation_wf_df <-
  df_short %>%
  group_by(Probation) %>%
  count(ChildWelfare) %>%
  rename(Var = Probation) %>%
  mutate(
    Pct = prop.table(n) * 100,
    Group = "Probation"
  )

bail_wf_df <-
  df_short %>%
  group_by(Bail) %>%
  count(ChildWelfare) %>%
  rename(Var = Bail) %>%
  mutate(
    Pct = prop.table(n) * 100,
    Group = "Bail"
  )

halfway_wf_df <-
  df_short %>%
  group_by(Halfway) %>%
  count(ChildWelfare) %>%
  rename(Var = Halfway) %>%
  mutate(
    Pct = prop.table(n) * 100,
    Group = "Halfway"
  )

housing_wf_df <-
  df_short %>%
  group_by(Housing) %>%
  count(ChildWelfare) %>%
  rename(Var = Housing) %>%
  mutate(
    Pct = prop.table(n) * 100,
    Group = "Housing"
  )

jail_wf_df <-
  df_short %>%
  group_by(Jail) %>%
  count(ChildWelfare) %>%
  rename(Var = Jail) %>%
  mutate(
    Pct = prop.table(n) * 100,
    Group = "Jail"
  )

cont_tables_wf <- rbind(
  police_wf_df,
  jail_wf_df,
  housing_wf_df,
  family_wf_df,
  court_wf_df,
  probation_wf_df,
  halfway_wf_df,
  bail_wf_df
)

ggplot(
  cont_tables_wf,
  aes(
    x = factor(Var),
    y = Pct,
    fill = factor(ChildWelfare) %>%
      fct_rev(),
    label = paste0(round(Pct), "%")
  )
) +
  geom_col(alpha = .75) +
  geom_text(
    position = position_stack(vjust = 0.5),
    size = 2.3
  ) +
  # scale_fill_brewer(palette = "GnBu", direction = -1) +
  scale_fill_tableau(
    palette = "Tableau 10",
    labels = c(
      "Very often", "Sometimes",
      "Occasionally", "Never"
    )
  ) +
  scale_x_discrete(labels = c(
    "Never", "Occasionally",
    "Sometimes", "Very often"
  )) +
  coord_flip() +
  facet_wrap(~Group, ncol = 2) +
  theme_minimal() +
  labs(
    x = "",
    fill = "Level of Child Welfare Contact",
    y = "% Distribution"
  ) +
  theme(legend.position = "bottom") +
  ggsave("publication_plots2/xtabs_welfare.pdf",
    width = 8, height = 6
  )
```


### Figure 1: The distribution of political identity by race, among those who have institutional contact


```{r}
df_short <-
  df_short %>%
  mutate(
    any_contact = ifelse(c(Police == 0 & Courts == 0 &
      Probation == 0 & Bail == 0 &
      Halfway == 0 & Housing == 0 &
      Housing == 0 & Jail == 0 &
      ChildWelfare == 0 & FamilyCourt == 0), 0, 1),
    lf_and_disc = ifelse(c(linkedfate == 1 & disc2 == 1), 1, 0),
    disc_no_lf = ifelse(c(linkedfate == 0 & disc2 == 1), 1, 0),
    degree_of_id = case_when(
      lf_and_disc == 1 ~ 3,
      disc_no_lf == 1 ~ 2,
      c(lf_and_disc == 0 & disc_no_lf == 0) ~ 1
    )
  )


df_contact <- df_short %>% filter(any_contact == 1)
race_dist <- round((prop.table(table(df_contact$race, df_contact$lf_and_disc), 1) * 100), digits = 2)


value <- rbind(
  race_dist[1, 1], race_dist[2, 1], race_dist[3, 1],
  race_dist[1, 2], race_dist[2, 2], race_dist[3, 2]
)


race <- rep(c("01", "02", "03"), times = 2)
level <- rep(c("01", "02"), each = 3)
x <- as.data.frame(cbind(value, race, level))

ggplot(x, aes(x = race, y = value, group = level, fill = level)) +
  geom_bar(stat = "identity", position = position_dodge()) +
  scale_fill_grey(
    start = 0.3, end = .8,
    labels = c("No Politicized ID", "Politicized ID")
  ) +
  scale_x_discrete(
    breaks = c("01", "02", "03"),
    labels = c("Black", "Asian", "Latino")
  ) +
  theme_bw(base_size = 12, base_family = "Times") +
  theme(strip.text.x = element_text(size = 12)) +
  theme(strip.background = element_rect(fill = "white")) +
  theme_minimal() +
  theme(
    legend.position = "bottom",
    legend.title = element_blank()
  ) +
  labs(
    y = "Percent", x = " ",
    title = c(" ", cex = 1)
  ) +
  ggsave(
    filename = "publication_plots2/politicizedID_contact_race.png",
    width = 8, height = 5
  )
```



## Main Analysis

### Figure 2: Main effects of contact on participation, full model

```{r}

index_base <-
  lm(polpart_index ~
  ChildWelfare + FamilyCourt + # contac
    Housing + Halfway + # conta
    Jail + Bail + # contact
    Probation + Courts + Police + # contact
    disc2 + linkedfate +
    external_efficacy + pol_interest + worship_att + # key control
    partyid + female + age18_29 + age30_39 + age40_64 + # control
    education + inc_less40k + inc_40_79k + race, # controls
  data = df_short
  )

preds <- summary(margins(index_base, change = "minmax"))

plot_lables <- c(
  "Age 18-29", "Age 30-39", "Age 40-64",
  "Education", "Political Efficacy", "Female",
  "Income 40-70K", "Income < 40K", "Party ID",
  "Political Interest", "Asian", "Latino",
  "Worship Attendance",
  "Linked Fate", "Discrimination", "Police",
  "Courts", "Probation", "Bail", "Jail",
  "Halfway", "Housing", "Family Court", "Child Welfare"
)

ggplot(
  preds,
  aes(
    x = factor %>%
      fct_relevel("linkedfate", "disc2",
        "Police", "Courts", "Probation",
        "Bail", "Jail", "Halfway",
        "Housing", "FamilyCourt", "Child Welfare",
        after = 20
      ),
    y = AME,
    ymin = AME - (1.6 * SE),
    ymax = AME + (1.6 * SE)
  )
) +
  geom_pointrange() +
  scale_x_discrete(labels = plot_lables) +
  geom_hline(yintercept = 0, linetype = "dashed", color = "grey") +
  coord_flip() +
  theme_bw() +
  labs(y = "Min-Max Predicted Change - Participation Index", x = "") +
  ggsave("publication_plots2/minmax_preds_main.png",
    width = 6, height = 6
  )
```

### Table A2: Regression Table for Figure 2

```{r, results = "asis"}
tab_labels <- c(
  "Child Welfare", "Family Court", "Housing",
  "Halfway", "Jail", "Bail", "Probation",
  "Courts", "Police", "Discrimination",
  "Linked Fate", "Political Efficacy", "Political Interest",
  "Worship Attendance", "Party ID", "Female",
  "Age 18-29", "Age 30-39", "Age 40-64",
  "Education", "Income < 40K", "Income 40-70K", "Asian", "Latino"
)

stargazer(index_base,
  no.space = TRUE, type = "html",
  covariate.labels = tab_labels,
  dep.var.labels = c("Political Participaton"),
  title = "The Impact of Institutional Contact on Political Participation",
  out = "publication_tables2/mod_base_index.tex",
  label = "tab:base_model",
  font.size = "scriptsize",
  column.sep.width = "2pt",
  omit.stat = c("ser", "f")
)
```

### Table A3 and A4:   The impact of discrimination and institutional contact on participation, among those with (A3) and without (A4) linked fate

```{r, results = "asis"}

# Creating Formula

model_index <-
  polpart_index ~
  contact * disc2 +
    ChildWelfare + FamilyCourt + # contact
    Housing + Halfway + # contact
    Jail + Bail + # contact
    Probation + Courts + Police + # contact
    external_efficacy + pol_interest + worship_att + # key control
    partyid + female + age18_29 + age30_39 + age40_64 + # control
    education + inc_less40k + inc_40_79k + race # controls

# Running loop - no linked fate

mod_plots_index_nolink <-
  df_tidy_full %>%
  filter(
    linkedfate == 0,
    type %in% c(
      "Police", "Courts", "Probation",
      "Bail", "Jail", "Halfway",
      "Housing", "FamilyCourt", "ChildWelfare"
    )
  ) %>%
  split(.$type) %>%
  map(~ lm(model_index, data = .))


# Running loop - linked fate

mod_plots_index_link <-
  df_tidy_full %>%
  filter(
    linkedfate == 1,
    type %in% c(
      "Police", "Courts", "Probation",
      "Bail", "Jail", "Halfway",
      "Housing", "FamilyCourt", "ChildWelfare"
    )
  ) %>%
  split(.$type) %>%
  map(~ lm(model_index, data = .))


tab_labels <- c(
  "Contact", "Discrimination", "Child Welfare", "Family Court", "Housing",
  "Halfway", "Jail", "Bail", "Probation",
  "Courts", "Police", "Political Efficacy", "Political Interest",
  "Worship Attendance", "Party ID", "Female",
  "Age 40-64", "Age 30-39", "Age 18-29",
  "Education", "Income < 40K", "Income 40-70K", "Asian", "Latino",
  "Contact X Disc"
)


stargazer(mod_plots_index_link,
  type = "html",
  dep.var.labels = "Political Participaton",
  column.labels = c(
    "Bail", "Court", "Family",
    "Halfway", "Housing", "Jail", "Police",
    "Probation", "Child Welfare"
  ),
  covariate.labels = tab_labels,
  title = "Moderation analysis: The impact of discrimination and institutional contact on participation, among those with linked fate ",
  out = "publication_tables2/mod_int_index_link.tex",
  label = "tab:mod_int_index_link",
  no.space = TRUE,
  font.size = "scriptsize",
  column.sep.width = "2pt",
  omit.stat = c("ser", "f")
)

stargazer(mod_plots_index_nolink,
  type = "html",
  dep.var.labels = "Political Participaton",
  column.labels = c(
    "Bail", "Court", "Family",
    "Halfway", "Housing", "Jail", "Police",
    "Probation", "Child Welfare"
  ),
  covariate.labels = tab_labels,
  title = "Moderation analysis: The impact of discrimination and institutional contact on participation, among those without linked fate ",
  out = "publication_tables2/mod_int_index_nolink.tex",
  label = "tab:mod_int_index_nolink",
  no.space = TRUE,
  font.size = "scriptsize",
  column.sep.width = "2pt",
  omit.stat = c("ser", "f")
)
```



### Figure 3: Plotting the interaction term among those with and without linked fate, excluding the base term for discrimination for clarity (figure 2)

```{r}

mod_plots_index_nolink %>%
  map(tidy) %>%
  enframe(name = "type") %>%
  unnest() %>%
  filter(term %in% c("contact", "disc2", "contact:disc2")) %>%
  mutate(subset = "No Linked Fate") ->
df_models_plot_nolink


mod_plots_index_link %>%
  map(tidy) %>%
  enframe(name = "type") %>%
  unnest() %>%
  filter(term %in% c("contact", "disc2", "contact:disc2")) %>%
  mutate(subset = "Linked Fate") ->
df_models_plot_link


df_models_all <- rbind(
  df_models_plot_link,
  df_models_plot_nolink
)


type_legend <- c(
  "Bail", "Court", "Family",
  "Halfway", "Housing", "Jail",
  "Police", "Probation", "Child Welfare"
)


shape_type <-


  ggplot(
    df_models_all %>% dplyr::filter(term != "disc2"),
    aes(
      x = term,
      y = estimate,
      shape = type %>%
        fct_relevel(
          "Police", "Courts", "Probation",
          "Bail", "Jail", "Halfway",
          "Housing", "FamilyCourt", "ChildWelfare"
        ) %>%
        fct_rev(),
      color = type %>%
        fct_relevel(
          "Police", "Courts", "Probation",
          "Bail", "Jail", "Halfway",
          "Housing", "FamilyCourt", "ChildWelfare"
        ) %>%
        fct_rev(),
      ymax = estimate + (1.6 * std.error),
      ymin = estimate - (1.6 * std.error)
    )
  ) +
  geom_pointrange(
    position = position_dodge(width = .75),
    size = .8
  ) +
  geom_hline(yintercept = 0, linetype = "dashed") +
  coord_flip() +
  facet_wrap(~subset) +
  scale_x_discrete(labels = c(
    "Contact (Base Term)",
    "Contact X Discrimination"
  )) +
  scale_shape_manual(values = seq(1:9)) +
  scale_color_tableau() +
  labs(
    x = "", y = "Estimate",
    shape = "Type of\n Contact",
    color = "Type of\n Contact"
  ) +
  theme_bw() +
  ggsave("publication_plots2/coefplot_all_v2.png",
    width = 7, height = 6.5
  )
```

### Figure 4: Predicted value of participation by type of institutional contact, among those with and without a politicized group identity


```{r results='asis'}

model_index <-
  polpart_index ~
  contact * disc2 +
    Police + Courts + # contact
    Probation + Bail + # contact
    Halfway + Housing + # contact
    Jail + ChildWelfare + # contact
    FamilyCourt + # contact
    external_efficacy + pol_interest + worship_att + # key control
    partyid + female + age18_29 + age30_39 + age40_64 + citizen + # control
    education + inc_less40k + inc_40_79k + latino + asian # controls

xhyp <- df_tidy_full %$%
  expand.grid(
    contact = seq(0, 3),
    disc2 = c(0, 1),
    Police = mean(Police, na.rm = T),
    Courts = mean(Courts, na.rm = T),
    Probation = mean(Probation, na.rm = T),
    Bail = mean(Bail, na.rm = T),
    Halfway = mean(Halfway, na.rm = T),
    Housing = mean(Housing, na.rm = T),
    Jail = mean(Jail, na.rm = T),
    ChildWelfare = mean(ChildWelfare, na.rm = T),
    FamilyCourt = mean(FamilyCourt, na.rm = T),
    external_efficacy = mean(external_efficacy, na.rm = T),
    pol_interest = mean(pol_interest, na.rm = T),
    worship_att = mean(worship_att, na.rm = T),
    partyid = mean(partyid, na.rm = T),
    female = mean(female, na.rm = T),
    age18_29 = mean(age18_29, na.rm = T),
    age30_39 = mean(age30_39, na.rm = T),
    age40_64 = mean(age40_64, na.rm = T),
    citizen = mean(citizen, na.rm = T),
    education = mean(education, na.rm = T),
    inc_less40k = mean(inc_less40k, na.rm = T),
    inc_40_79k = mean(inc_40_79k, na.rm = T),
    latino = mean(latino, na.rm = T),
    asian = mean(asian, na.rm = T)
  )



df_preds_index_nolink <-
  df_tidy_full %>%
  filter(linkedfate == 0) %>%
  split(.$type) %>%
  map(~ lm(model_index, data = .)) %>%
  map(~ augment(., newdata = xhyp, se_fit = TRUE)) %>%
  enframe(name = "type") %>%
  unnest() %>%
  select(type, contact, disc2, .fitted, .se.fit) %>%
  mutate(subset = "No Linked Fate")

df_preds_index_link <-
  df_tidy_full %>%
  filter(linkedfate == 1) %>%
  split(.$type) %>%
  map(~ lm(model_index, data = .)) %>%
  map(~ augment(., newdata = xhyp, se_fit = TRUE)) %>%
  enframe(name = "type") %>%
  unnest() %>%
  select(type, contact, disc2, .fitted, .se.fit) %>%
  mutate(subset = "Linked Fate")


df_preds <- bind_rows(df_preds_index_link, df_preds_index_nolink)

ggplot(
  df_preds %>% filter(
    disc2 == 1,
    type %in%
      c(
        "Police", "Courts", "Probation",
        "Bail", "Jail", "Halfway",
        "Housing", "FamilyCourt", "ChildWelfare"
      )
  ),
  aes(
    x = contact, y = .fitted,
    #    color = factor(subset),
    #    fill = factor(subset),
    shape = factor(subset),
    ymin = .fitted - 1.6 * (.se.fit),
    ymax = .fitted + 1.6 * (.se.fit)
  )
) +
  geom_pointrange(size = .7) +
  geom_line() +
  facet_wrap(~ type %>%
    fct_relevel(
      "Police", "Courts", "Probation",
      "Bail", "Jail", "Halfway",
      "Housing", "FamilyCourt", "ChildWelfare"
    ) %>%
    fct_rev(), ncol = 3) +
  scale_fill_grey(
    start = 0.1, end = .7
  ) +
  scale_color_grey(
    start = 0.1, end = .7
  ) +
  theme_minimal() +
  theme(legend.position = "bottom") +
  labs(
    x = "Level of Contact", y = "Predicted Participation",
    color = "",
    fill = "",
    shape = ""
  ) +
  ggsave("publication_plots2/pred_probs_main_all.png",
    width = 6, height = 6
  )
```


###  Table A5, A6:   The impact of discrimination and institutional contact on participation, among Black Americans with (A5) and without (A6) linked fate

```{r results='asis'}

model_index_race <-
  polpart_index ~
  contact * disc2 +
    Police + Courts + # contact
    Probation + Bail + # contact
    Halfway + Housing + # contact
    Jail + ChildWelfare + # contact
    FamilyCourt + # contact
    external_efficacy + pol_interest + worship_att + # key control
    partyid + female + age18_29 + age30_39 + age40_64 + # control
    education + inc_less40k + inc_40_79k # controls

models_index_link_black <-
  df_tidy_full %>%
  filter(
    c(linkedfate == 1 & black == 1),
    type %in% c(
      "Police", "Courts", "Probation",
      "Bail", "Jail", "Halfway",
      "Housing", "FamilyCourt", "ChildWelfare"
    )
  ) %>%
  split(list(.$type)) %>%
  map(~ lm(model_index_race, data = .))

tab_labels <- c(
  "Contact", "Discrimination", "Police", "Courts", "Probation", "Bail",
  "Halfway", "Housing", "Jail", "ChildWelfare", "Family Court", "Political Efficacy",
  "Political Interest",
  "Worship Attendance", "Party ID", "Female",
  "Age 18-29", "Age 30-39", "Age 40-64",
  "Education", "Income < 40K", "Income 40-70K",
  "Contact X Disc"
)

stargazer(models_index_link_black,
  type = "html",
  dep.var.labels = "Political Participaton",
  column.labels = c(
    "Bail", "Court", "Family",
    "Halfway", "Housing", "Jail", "Police",
    "Probation", "Welafare"
  ),
  covariate.labels = tab_labels,
  title = "Moderation analysis: The impact of discrimination and institutional contact on participation, among Black Americans with linked fate ",
  out = "publication_tables2/mod_int_index_link_black.tex",
  label = "tab:mod_int_index_link_black",
  no.space = TRUE,
  font.size = "scriptsize",
  column.sep.width = "2pt",
  omit.stat = c("ser", "f")
)


models_index_nolink_black <-
  df_tidy_full %>%
  filter(
    c(linkedfate == 0 & black == 1),
    type %in% c(
      "Police", "Courts", "Probation",
      "Bail", "Jail", "Halfway",
      "Housing", "FamilyCourt", "ChildWelfare"
    )
  ) %>%
  split(list(.$type, .$black)) %>%
  map(~ lm(model_index_race, data = .))


stargazer(models_index_nolink_black,
  type = "html",
  dep.var.labels = "Political Participaton",
  column.labels = c(
    "Bail", "Court", "Family",
    "Halfway", "Housing", "Jail", "Police",
    "Probation", "Welafare"
  ),
  covariate.labels = tab_labels,
  title = "Moderation analysis: The impact of discrimination and institutional contact on participation, among Black Americans without linked fate ",
  out = "publication_tables2/mod_int_index_nolink_black.tex",
  label = "tab:mod_int_index_nolink_black",
  no.space = TRUE,
  font.size = "scriptsize",
  column.sep.width = "2pt",
  omit.stat = c("ser", "f")
)
```

###  Table A7, A8:   The impact of discrimination and institutional contact on participation, among Latinos with (A7) and without (A8) linked fate

``` {r results='asis'}
models_index_link_latino <-
  df_tidy_full %>%
  filter(
    c(linkedfate == 1 & latino == 1),
    type %in% c(
      "Police", "Courts", "Probation",
      "Bail", "Jail", "Halfway",
      "Housing", "FamilyCourt", "ChildWelfare"
    )
  ) %>%
  split(list(.$type)) %>%
  map(~ lm(model_index_race, data = .))

stargazer(models_index_link_latino,
  type = "html",
  dep.var.labels = "Political Participaton",
  column.labels = c(
    "Bail", "Court", "Family",
    "Halfway", "Housing", "Jail", "Police",
    "Probation", "Welafare"
  ),
  covariate.labels = tab_labels,
  title = "Moderation analysis: The impact of discrimination and institutional contact on participation, among Latinos with linked fate ",
  out = "publication_tables2/mod_int_index_link_latino.tex",
  label = "tab:mod_int_index_link_latino",
  no.space = TRUE,
  font.size = "scriptsize",
  column.sep.width = "2pt",
  omit.stat = c("ser", "f")
)



models_index_nolink_latino <-
  df_tidy_full %>%
  filter(
    c(linkedfate == 0 & latino == 1),
    type %in% c(
      "Police", "Courts", "Probation",
      "Bail", "Jail", "Halfway",
      "Housing", "FamilyCourt", "ChildWelfare"
    )
  ) %>%
  split(list(.$type, .$latino)) %>%
  map(~ lm(model_index_race, data = .))


stargazer(models_index_nolink_latino,
  type = "html",
  dep.var.labels = "Political Participaton",
  column.labels = c(
    "Bail", "Court", "Family",
    "Halfway", "Housing", "Jail", "Police",
    "Probation", "Welafare"
  ),
  covariate.labels = tab_labels,
  title = "Moderation analysis: The impact of discrimination and institutional contact on participation, among Latinos without linked fate ",
  out = "publication_tables2/mod_int_index_nolink_latino.tex",
  label = "tab:mod_int_index_nolink_latino",
  no.space = TRUE,
  font.size = "scriptsize",
  column.sep.width = "2pt",
  omit.stat = c("ser", "f")
)
```

###  Table A9, A10:   The impact of discrimination and institutional contact on participation, among Asian Americans with (A9) and without (A10) linked fate

``` {r results='asis'}

models_index_link_asian <-
  df_tidy_full %>%
  filter(
    c(linkedfate == 1 & asian == 1),
    type %in% c(
      "Police", "Courts", "Probation",
      "Bail", "Jail", "Halfway",
      "Housing", "FamilyCourt", "ChildWelfare"
    )
  ) %>%
  split(list(.$type)) %>%
  map(~ lm(model_index_race, data = .))

stargazer(models_index_link_asian,
  type = "html",
  dep.var.labels = "Political Participaton",
  column.labels = c(
    "Bail", "Court", "Family",
    "Halfway", "Housing", "Jail", "Police",
    "Probation", "Welafare"
  ),
  covariate.labels = tab_labels,
  title = "Moderation analysis: The impact of discrimination and institutional contact on participation, among Asians with linked fate ",
  out = "publication_tables2/mod_int_index_link_asian.tex",
  label = "tab:mod_int_index_link_asian",
  no.space = TRUE,
  font.size = "scriptsize",
  column.sep.width = "2pt",
  omit.stat = c("ser", "f")
)

models_index_nolink_asian <-
  df_tidy_full %>%
  filter(
    c(linkedfate == 0 & asian == 1),
    type %in% c(
      "Police", "Courts", "Probation",
      "Bail", "Jail", "Halfway",
      "Housing", "FamilyCourt", "ChildWelfare"
    )
  ) %>%
  split(list(.$type, .$asian)) %>%
  map(~ lm(model_index_race, data = .))


stargazer(models_index_nolink_asian,
  type = "html",
  dep.var.labels = "Political Participaton",
  column.labels = c(
    "Bail", "Court", "Family",
    "Halfway", "Housing", "Jail", "Police",
    "Probation", "Welafare"
  ),
  covariate.labels = tab_labels,
  title = "Moderation analysis: The impact of discrimination and institutional contact on participation, among Asians without linked fate ",
  out = "publication_tables2/mod_int_index_nolink_asian.tex",
  label = "tab:mod_int_index_nolink_asian",
  no.space = TRUE,
  font.size = "scriptsize",
  column.sep.width = "2pt",
  omit.stat = c("ser", "f")
)
```

### Figure 5: The moderating effect of a politicized group identity on involuntary institutional contact and participation by race.

```{r}

model_index_race <-
  polpart_index ~
  contact * disc2 +
    Police + Courts + # contact
    Probation + Bail + # contact
    Halfway + Housing + # contact
    Jail + ChildWelfare + # contact
    FamilyCourt + # contact
    external_efficacy + pol_interest + worship_att + # key control
    partyid + female + age18_29 + age30_39 + age40_64 + citizen + # control
    education + inc_less40k + inc_40_79k # controls

models_index_link_race <-
  df_tidy_full %>%
  filter(
    linkedfate == 1,
    type %in% c(
      "Police", "Courts", "Probation",
      "Bail", "Jail", "Halfway",
      "Housing", "FamilyCourt", "ChildWelfare"
    )
  ) %>%
  split(list(.$type, .$race)) %>%
  map(~ lm(model_index_race, data = .)) %>%
  map(tidy)

models_index_nolink_race <-
  df_tidy_full %>%
  filter(
    linkedfate == 0,
    type %in% c(
      "Police", "Courts", "Probation",
      "Bail", "Jail", "Halfway",
      "Housing", "FamilyCourt", "ChildWelfare"
    )
  ) %>%
  split(list(.$type, .$race)) %>%
  map(~ lm(model_index_race, data = .)) %>%
  map(tidy)

models_index_nolink_race %>%
  enframe(name = "type") %>%
  unnest() %>%
  filter(term %in% c("contact", "disc2", "contact:disc2")) %>%
  mutate(subset = "No Linked Fate") %>%
  separate(type, c("type", "race"), extra = "merge") ->
df_models_plot_nolink_race

models_index_link_race %>%
  enframe(name = "type") %>%
  unnest() %>%
  filter(term %in% c("contact", "disc2", "contact:disc2")) %>%
  mutate(subset = "Linked Fate") %>%
  separate(type, c("type", "race"), extra = "merge") ->
df_models_plot_link_race

df_models_all_race <- rbind(
  df_models_plot_link_race,
  df_models_plot_nolink_race
)


df_models_all_race <- df_models_all_race %>%
  mutate(race1 = case_when(
    race == "black" ~ "1",                                                                race == "latino" ~ "2",
    race == "asian" ~ "3"
  ))

lab <- c(
  "1" = "Black",
  "2" = "Latino",
  "3" = "Asian"
)

type_legend <- c(
  "Bail", "Court", "Family",
  "Halfway", "Housing", "Jail",
  "Police", "Probation", "ChildWelfare"
)

ggplot(
  df_models_all_race %>%
    filter(term == "contact:disc2" & race != "index$"),
  aes(
    x = term %>%
      fct_relevel("contact:disc2") %>%
      fct_rev(),
    y = estimate,
    color = type %>%
      fct_relevel(
        "Police", "Courts", "Probation",
        "Bail", "Jail", "Halfway",
        "Housing", "FamilyCourt", "ChildWelfare"
      ) %>%
      fct_rev(),
    shape = type %>%
      fct_relevel(
        "Police", "Courts", "Probation",
        "Bail", "Jail", "Halfway",
        "Housing", "FamilyCourt", "ChildWelfare"
      ) %>%
      fct_rev(),
    ymax = estimate + (1.6 * std.error),
    ymin = estimate - (1.6 * std.error)
  )
) +
  geom_pointrange(
    position = position_dodge(width = .75),
    size = .8
  ) +
  geom_hline(yintercept = 0, linetype = "dashed") +
  coord_flip() +
  facet_grid(subset ~ race1,
    # scales = "free_x",
    labeller = labeller(race1 = lab)
  ) +
  scale_x_discrete(labels = c("Contact X Discrimination")) +
  scale_shape_manual(values = seq(1:9)) +
  scale_color_tableau() +
  labs(
    x = "",
    y = "Estimate",
    shape = "Type of\n Contact",
    color = "Type of\n Contact"
  ) +
  theme_bw() +
  ggsave("publication_plots2/coefplot_race_subgroups.png",
    width = 7, height = 6.5
  )
```



### Figure A4: The impact of a politicized group identity and institutional contact on self-reported voting by race

```{r}

model_vote_race <-
  voted ~
  contact * disc2 +
    Police + Courts + # contact
    Probation + Bail + # contact
    Halfway + Housing + # contact
    Jail + ChildWelfare + # contact
    FamilyCourt + # contac
    external_efficacy + pol_interest + worship_att + # key control
    partyid + female + age18_29 + age30_39 + age40_64 + citizen + # control
    education + inc_less40k + inc_40_79k # controls

models_vote_link_race <-
  df_tidy_full %>%
  filter(
    linkedfate == 1,
    type %in% c(
      "Police", "Courts", "Probation",
      "Bail", "Jail", "Halfway",
      "Housing", "FamilyCourt", "ChildWelfare"
    )
  ) %>%
  split(list(.$type, .$race)) %>%
  map(~ glm(model_vote_race, data = ., family = "binomial")) %>%
  map(tidy)

models_vote_nolink_race <-
  df_tidy_full %>%
  filter(
    linkedfate == 0,
    type %in% c(
      "Police", "Courts", "Probation",
      "Bail", "Jail", "Halfway",
      "Housing", "FamilyCourt", "ChildWelfare"
    )
  ) %>%
  split(list(.$type, .$race)) %>%
  map(~ glm(model_vote_race, data = ., family = "binomial")) %>%
  map(tidy)

models_vote_nolink_race %>%
  enframe(name = "type") %>%
  unnest() %>%
  filter(term %in% c("contact", "disc2", "contact:disc2")) %>%
  mutate(subset = "No Linked Fate") %>%
  separate(type, c("type", "race"), extra = "merge") ->
df_models_plot_nolink_race

models_vote_link_race %>%
  enframe(name = "type") %>%
  unnest() %>%
  filter(term %in% c("contact", "disc2", "contact:disc2")) %>%
  mutate(subset = "Linked Fate") %>%
  separate(type, c("type", "race"), extra = "merge") ->
df_models_plot_link_race

df_models_all_race <- rbind(
  df_models_plot_link_race,
  df_models_plot_nolink_race
)


df_models_all_race_vote <- df_models_all_race %>%
  mutate(race1 = case_when(
    race == "black" ~ "1",
    race == "latino" ~ "2",
    race == "asian" ~ "3"
  ))

lab <- c(
  "1" = "Black",
  "2" = "Latino",
  "3" = "Asian"
)

type_legend <- c(
  "Bail", "Court", "Family",
  "Halfway", "Housing", "Jail",
  "Police", "Probation", "ChildWelfare"
)

ggplot(
  df_models_all_race_vote %>%
    filter(term == "contact:disc2" &
      race != "index$"),
  aes(
    x = term %>%
      fct_relevel("contact:disc2") %>%
      fct_rev(),
    y = estimate,
    color = type %>%
      fct_relevel(
        "Police", "Courts",
        "Probation",
        "Bail", "Jail",
        "Halfway",
        "Housing", "FamilyCourt",
        "ChildWelfare"
      ) %>%
      fct_rev(),
    ymax = estimate + (1.96 * std.error),
    ymin = estimate - (1.96 * std.error)
  )
) +
  geom_pointrange(
    position = position_dodge(width = .75),
    size = .6
  ) +
  geom_hline(yintercept = 0, linetype = "dashed") +
  coord_flip() +
  facet_grid(subset ~ race1,
    # scales = "free_x",
    labeller = labeller(race1 = lab)
  ) +
  scale_x_discrete(labels = c("Contact X Discrimination")) +
  scale_color_tableau() +
  labs(
    x = "Variable", y = "Estimated Vote Pr",
    color = "Type of\n Contact"
  ) +
  theme_bw() +
  ggsave("publication_plots2/vote_plot_race.pdf",
    width = 8, height = 6
  )
```


## Robustness check


### Figure A4: Breaking out the participation battery

```{r, results = "asis"}

y_var <- list("campaign_vol", "donate", "campaign_button", "contact_rep", "contact_any", "work_others", "meeting", "protest", "petition", "boycott")


x_var <- list(c(
  "contact*disc2",
  "ChildWelfare", "FamilyCourt", # contact
  "Housing", "Halfway", # contact
  "Jail", "Bail", # contact
  "Probation", "Courts", "Police", # contact
  "external_efficacy", "pol_interest", "worship_att", # key control
  "partyid", "female", "age18_29", "age30_39", "age40_64", # control
  "education", "inc_less40k", "inc_40_79k", "race"
))

z_type <- c(
  "ChildWelfare", "FamilyCourt", # contact
  "Housing", "Halfway", # contact
  "Jail", "Bail", # contact
  "Probation", "Courts", "Police", "ChildWelfare", "FamilyCourt"
)



mod_plots_index_nolink <-
  forms <- map2(x_var, y_var, ~ (paste(.y, "~", paste(.x, collapse = " + "))))
```

#### "ChildWelfare",

```{r}

mod_plots_index_nolink <-
  map2(x_var, y_var, ~ lm(as.formula(paste(.y, "~", paste(.x, collapse = " + "))),
    data = df_tidy_full %>%
      filter(
        linkedfate == 0,
        type == "ChildWelfare"
      )
  ))

mod_plots_index_link <-
  map2(x_var, y_var, ~ lm(as.formula(paste(.y, "~", paste(.x, collapse = " + "))),
    data = df_tidy_full %>%
      filter(
        linkedfate == 1,
        type == "ChildWelfare"
      )
  ))


mod_plots_index_nolink %>%
  map(tidy) %>%
  enframe(name = "Type_Participation") %>%
  unnest() %>%
  filter(term %in% c("contact", "disc2", "contact:disc2")) %>%
  mutate(subset = "No Linked Fate") ->
df_models_plot_nolink

mod_plots_index_link %>%
  map(tidy) %>%
  enframe(name = "Type_Participation") %>%
  unnest() %>%
  filter(term %in% c("contact", "disc2", "contact:disc2")) %>%
  mutate(subset = "Linked Fate") ->
df_models_plot_link

df_models_ChildWelfare <- rbind(
  df_models_plot_link,
  df_models_plot_nolink
) %>%
  mutate(Type_of_Contact = "ChildWelfare")
```


#### "FamilyCourt", 

```{r}

mod_plots_index_nolink <-
  map2(x_var, y_var, ~ lm(as.formula(paste(.y, "~", paste(.x, collapse = " + "))),
    data = df_tidy_full %>%
      filter(
        linkedfate == 0,
        type == "FamilyCourt"
      )
  ))

mod_plots_index_link <-
  map2(x_var, y_var, ~ lm(as.formula(paste(.y, "~", paste(.x, collapse = " + "))),
    data = df_tidy_full %>%
      filter(
        linkedfate == 1,
        type == "FamilyCourt"
      )
  ))


mod_plots_index_nolink %>%
  map(tidy) %>%
  enframe(name = "Type_Participation") %>%
  unnest() %>%
  filter(term %in% c("contact", "disc2", "contact:disc2")) %>%
  mutate(subset = "No Linked Fate") ->
df_models_plot_nolink


mod_plots_index_link %>%
  map(tidy) %>%
  enframe(name = "Type_Participation") %>%
  unnest() %>%
  filter(term %in% c("contact", "disc2", "contact:disc2")) %>%
  mutate(subset = "Linked Fate") ->
df_models_plot_link


df_models_FamilyCourt <- rbind(
  df_models_plot_link,
  df_models_plot_nolink
) %>%
  mutate(Type_of_Contact = "FamilyCourt")
```


#### "Housing"

```{r}

mod_plots_index_nolink <-
  map2(x_var, y_var, ~ lm(as.formula(paste(.y, "~", paste(.x, collapse = " + "))),
    data = df_tidy_full %>%
      filter(
        linkedfate == 0,
        type == "Housing"
      )
  ))

mod_plots_index_link <-
  map2(x_var, y_var, ~ lm(as.formula(paste(.y, "~", paste(.x, collapse = " + "))),
    data = df_tidy_full %>%
      filter(
        linkedfate == 1,
        type == "Housing"
      )
  ))


mod_plots_index_nolink %>%
  map(tidy) %>%
  enframe(name = "Type_Participation") %>%
  unnest() %>%
  filter(term %in% c("contact", "disc2", "contact:disc2")) %>%
  mutate(subset = "No Linked Fate") ->
df_models_plot_nolink


mod_plots_index_link %>%
  map(tidy) %>%
  enframe(name = "Type_Participation") %>%
  unnest() %>%
  filter(term %in% c("contact", "disc2", "contact:disc2")) %>%
  mutate(subset = "Linked Fate") ->
df_models_plot_link


df_models_Housing <- rbind(
  df_models_plot_link,
  df_models_plot_nolink
) %>%
  mutate(Type_of_Contact = "Housing")
```

#### "Halfway",   

```{r}


mod_plots_index_nolink <-
  map2(x_var, y_var, ~ lm(as.formula(paste(.y, "~", paste(.x, collapse = " + "))),
    data = df_tidy_full %>%
      filter(
        linkedfate == 0,
        type == "Halfway"
      )
  ))

mod_plots_index_link <-
  map2(x_var, y_var, ~ lm(as.formula(paste(.y, "~", paste(.x, collapse = " + "))),
    data = df_tidy_full %>%
      filter(
        linkedfate == 1,
        type == "Halfway"
      )
  ))


mod_plots_index_nolink %>%
  map(tidy) %>%
  enframe(name = "Type_Participation") %>%
  unnest() %>%
  filter(term %in% c("contact", "disc2", "contact:disc2")) %>%
  mutate(subset = "No Linked Fate") ->
df_models_plot_nolink


mod_plots_index_link %>%
  map(tidy) %>%
  enframe(name = "Type_Participation") %>%
  unnest() %>%
  filter(term %in% c("contact", "disc2", "contact:disc2")) %>%
  mutate(subset = "Linked Fate") ->
df_models_plot_link


df_models_Halfway <- rbind(
  df_models_plot_link,
  df_models_plot_nolink
) %>%
  mutate(Type_of_Contact = "Halfway")
```

#### "Jail"

```{r}

mod_plots_index_nolink <-
  map2(x_var, y_var, ~ lm(as.formula(paste(.y, "~", paste(.x, collapse = " + "))),
    data = df_tidy_full %>%
      filter(
        linkedfate == 0,
        type == "Jail"
      )
  ))

mod_plots_index_link <-
  map2(x_var, y_var, ~ lm(as.formula(paste(.y, "~", paste(.x, collapse = " + "))),
    data = df_tidy_full %>%
      filter(
        linkedfate == 1,
        type == "Jail"
      )
  ))


mod_plots_index_nolink %>%
  map(tidy) %>%
  enframe(name = "Type_Participation") %>%
  unnest() %>%
  filter(term %in% c("contact", "disc2", "contact:disc2")) %>%
  mutate(subset = "No Linked Fate") ->
df_models_plot_nolink


mod_plots_index_link %>%
  map(tidy) %>%
  enframe(name = "Type_Participation") %>%
  unnest() %>%
  filter(term %in% c("contact", "disc2", "contact:disc2")) %>%
  mutate(subset = "Linked Fate") ->
df_models_plot_link


df_models_Jail <- rbind(
  df_models_plot_link,
  df_models_plot_nolink
) %>%
  mutate(Type_of_Contact = "Jail")
```


#### "Bail", 

```{r}


mod_plots_index_nolink <-
  map2(x_var, y_var, ~ lm(as.formula(paste(.y, "~", paste(.x, collapse = " + "))),
    data = df_tidy_full %>%
      filter(
        linkedfate == 0,
        type == "Bail"
      )
  ))

mod_plots_index_link <-
  map2(x_var, y_var, ~ lm(as.formula(paste(.y, "~", paste(.x, collapse = " + "))),
    data = df_tidy_full %>%
      filter(
        linkedfate == 1,
        type == "Bail"
      )
  ))


mod_plots_index_nolink %>%
  map(tidy) %>%
  enframe(name = "Type_Participation") %>%
  unnest() %>%
  filter(term %in% c("contact", "disc2", "contact:disc2")) %>%
  mutate(subset = "No Linked Fate") ->
df_models_plot_nolink


mod_plots_index_link %>%
  map(tidy) %>%
  enframe(name = "Type_Participation") %>%
  unnest() %>%
  filter(term %in% c("contact", "disc2", "contact:disc2")) %>%
  mutate(subset = "Linked Fate") ->
df_models_plot_link


df_models_Bail <- rbind(
  df_models_plot_link,
  df_models_plot_nolink
) %>%
  mutate(Type_of_Contact = "Bail")
```

#### "Probation",
```{r}


mod_plots_index_nolink <-
  map2(x_var, y_var, ~ lm(as.formula(paste(.y, "~", paste(.x, collapse = " + "))),
    data = df_tidy_full %>%
      filter(
        linkedfate == 0,
        type == "Probation"
      )
  ))


mod_plots_index_link <-
  map2(x_var, y_var, ~ lm(as.formula(paste(.y, "~", paste(.x, collapse = " + "))),
    data = df_tidy_full %>%
      filter(
        linkedfate == 1,
        type == "Probation"
      )
  ))


mod_plots_index_nolink %>%
  map(tidy) %>%
  enframe(name = "Type_Participation") %>%
  unnest() %>%
  filter(term %in% c("contact", "disc2", "contact:disc2")) %>%
  mutate(subset = "No Linked Fate") ->
df_models_plot_nolink


mod_plots_index_link %>%
  map(tidy) %>%
  enframe(name = "Type_Participation") %>%
  unnest() %>%
  filter(term %in% c("contact", "disc2", "contact:disc2")) %>%
  mutate(subset = "Linked Fate") ->
df_models_plot_link


df_models_Probation <- rbind(
  df_models_plot_link,
  df_models_plot_nolink
) %>%
  mutate(Type_of_Contact = "Probation")
```


#### "Courts"
 
```{r}


mod_plots_index_nolink <-
  map2(x_var, y_var, ~ lm(as.formula(paste(.y, "~", paste(.x, collapse = " + "))),
    data = df_tidy_full %>%
      filter(
        linkedfate == 0,
        type == "Courts"
      )
  ))

mod_plots_index_link <-
  map2(x_var, y_var, ~ lm(as.formula(paste(.y, "~", paste(.x, collapse = " + "))),
    data = df_tidy_full %>%
      filter(
        linkedfate == 1,
        type == "Courts"
      )
  ))

mod_plots_index_nolink %>%
  map(tidy) %>%
  enframe(name = "Type_Participation") %>%
  unnest() %>%
  filter(term %in% c("contact", "disc2", "contact:disc2")) %>%
  mutate(subset = "No Linked Fate") ->
df_models_plot_nolink


mod_plots_index_link %>%
  map(tidy) %>%
  enframe(name = "Type_Participation") %>%
  unnest() %>%
  filter(term %in% c("contact", "disc2", "contact:disc2")) %>%
  mutate(subset = "Linked Fate") ->
df_models_plot_link


df_models_Courts <- rbind(
  df_models_plot_link,
  df_models_plot_nolink
) %>%
  mutate(Type_of_Contact = "Courts")
```

#### "Police",
```{r}


mod_plots_index_nolink <-
  map2(x_var, y_var, ~ lm(as.formula(paste(.y, "~", paste(.x, collapse = " + "))),
    data = df_tidy_full %>%
      filter(
        linkedfate == 0,
        type == "Police"
      )
  ))

mod_plots_index_link <-
  map2(x_var, y_var, ~ lm(as.formula(paste(.y, "~", paste(.x, collapse = " + "))),
    data = df_tidy_full %>%
      filter(
        linkedfate == 1,
        type == "Police"
      )
  ))


mod_plots_index_nolink %>%
  map(tidy) %>%
  enframe(name = "Type_Participation") %>%
  unnest() %>%
  filter(term %in% c("contact", "disc2", "contact:disc2")) %>%
  mutate(subset = "No Linked Fate") ->
df_models_plot_nolink


mod_plots_index_link %>%
  map(tidy) %>%
  enframe(name = "Type_Participation") %>%
  unnest() %>%
  filter(term %in% c("contact", "disc2", "contact:disc2")) %>%
  mutate(subset = "Linked Fate") ->
df_models_plot_link
```


##### Ploting Figure A4

```{r}
df_models_Police <- rbind(
  df_models_plot_link,
  df_models_plot_nolink
) %>%
  mutate(Type_of_Contact = "Police")

df_models_all <- rbind(
  df_models_ChildWelfare, df_models_FamilyCourt, df_models_Housing,
  df_models_Halfway, df_models_Jail, df_models_Bail, df_models_Probation,
  df_models_Courts, df_models_Police
)


y_labels <- c(
  `1` = "Volunteer",
  `2` = "Donate",
  `3` = "Button",
  `4` = "Contact Rep",
  `5` = "Contact Any",
  `6` = "Work Others",
  `7` = "Meeting",
  `8` = "Protest",
  `9` = "Petition",
  `10` = "Boycott"
)

ggplot(
  df_models_all %>% filter(term == "contact:disc2"),
  aes(
    x = term,
    y = estimate,
    color = factor(Type_of_Contact),
    ymax = estimate + (1.6 * std.error),
    ymin = estimate - (1.6 * std.error)
  )
) +
  geom_pointrange(
    position = position_dodge(width = .75),
    size = .6
  ) +
  geom_hline(yintercept = 0, linetype = "dashed") +
  scale_color_tableau() +
  scale_x_discrete(breaks = NULL) +
  coord_flip() +
  facet_grid(Type_Participation ~ subset, labeller = labeller(Type_Participation = y_labels)) +
  labs(
    x = "", y = "Estimate",
    color = "Type of Contact"
  ) +
  theme_bw() +
  ggsave("publication_plots2/coefplot_robust_itemized2.pdf",
    width = 7, height = 9.5
  )
```







```{r results='asis'}

model_index <-
  polpart_index ~
  contact * disc2 +
    ChildWelfare + FamilyCourt + # contact
    Housing + Halfway + # contact
    Jail + Bail + # contact
    Probation + Courts + Police + # contact
    external_efficacy + pol_interest + worship_att + # key control
    partyid + female + age18_29 + age30_39 + age40_64 + # control
    education + inc_less40k + inc_40_79k + race # controls


mod_plots_index_nolink <-
  df_tidy_full %>%
  filter(
    pan_id == 1, c(race == "asian" | race == "latino"),
    type %in% c(
      "Police", "Courts", "Probation",
      "Bail", "Jail", "Halfway",
      "Housing", "FamilyCourt", "ChildWelfare"
    )
  ) %>%
  split(.$type) %>%
  map(~ lm(model_index, data = .))

mod_plots_index_link <-
  df_tidy_full %>%
  filter(
    pan_id == 4, c(race == "asian" | race == "latino"),
    type %in% c(
      "Police", "Courts", "Probation",
      "Bail", "Jail", "Halfway",
      "Housing", "FamilyCourt", "ChildWelfare"
    )
  ) %>%
  split(.$type) %>%
  map(~ lm(model_index, data = .))


tab_labels <- c(
  "Contact", "Discrimination", "ChildWelfare", "Family Court", "Housing",
  "Halfway", "Jail", "Bail", "Probation",
  "Courts", "Police", "Political Efficacy", "Political Interest",
  "Worship Attendance", "Party ID", "Female",
  "Age 40-64", "Age 30-39", "Age 18-29",
  "Education", "Income < 40K", "Income 40-70K", "Latino",
  "Contact X Disc"
)


stargazer(mod_plots_index_link,
  type = "html",
  dep.var.labels = "Political Participaton",
  column.labels = c(
    "Bail", "Court", "Family",
    "Halfway", "Housing", "Jail", "Police",
    "Probation", "ChildWelfare"
  ),
  covariate.labels = tab_labels,
  title = "Moderation analysis: The impact of discrimination and institutional contact on participation, among Asians and Latinos with a strong panethnic identity",
  out = "publication_tables2/mod_int_index_panethnic.tex",
  label = "tab:mod_int_index_panethnic",
  no.space = TRUE,
  font.size = "scriptsize",
  column.sep.width = "2pt",
  omit.stat = c("ser", "f")
)

stargazer(mod_plots_index_nolink,
  type = "html",
  dep.var.labels = "Political Participaton",
  column.labels = c(
    "Bail", "Court", "Family",
    "Halfway", "Housing", "Jail", "Police",
    "Probation", "Welafare"
  ),
  covariate.labels = tab_labels,
  title = "Moderation analysis: The impact of discrimination and institutional contact on participation, among Asians and Latinos without a strong panethnic identity",
  out = "publication_tables2/mod_int_index_nopanethnic.tex",
  label = "tab:mod_int_index_nopanethnic",
  no.space = TRUE,
  font.size = "scriptsize",
  column.sep.width = "2pt",
  omit.stat = c("ser", "f")
)
```


### Table A11:  Moderation analysis: The impact of discrimination and an index of institutional contacts on participation, among those with and without linked fate

```{r, results = 'asis'}

model1 <- lm(polpart_index ~ disc2 * ContactIndex + external_efficacy + pol_interest + worship_att +
  partyid + female + age18_29 + age30_39 + age40_64 + education + inc_less40k + inc_40_79k + race,
data = filter(df, linkedfate == 0)
)

model2 <- lm(polpart_index ~ disc2 * ContactIndex + external_efficacy + pol_interest + worship_att +
  partyid + female + age18_29 + age30_39 + age40_64 + education + inc_less40k + inc_40_79k + race,
data = filter(df, linkedfate == 1)
)


tab_labels <- c(
  "Discrimination", "Contact Index", "Political Efficacy", "Political Interest",
  "Worship Attendance", "Party ID", "Female",
  "Age 40-64", "Age 30-39", "Age 18-29",
  "Education", "Income < 40K", "Income 40-70K", "Asian", "Latino",
  "Contact X Disc"
)


stargazer(model2, model1,
  type = "html",
  dep.var.labels = "Political Participaton",
  column.labels = c("Linked Fate", "No Linked Fate"),
  covariate.labels = tab_labels,
  title = "Moderation analysis: The impact of discrimination and an index of institutional contacts on participation, among those with and without linked fate",
  out = "publication_tables2/ContactIndex.tex",
  label = "tab:ContactIndex",
  no.space = TRUE,
  font.size = "scriptsize",
  column.sep.width = "2pt",
  omit.stat = c("ser", "f")
)
```




###  Table A12 and A13: Moderation analysis: The impact of discrimination and institutional contact on participation, among Asians and Latinos with (A12) and without (A13) a strong panethnic identity

```{r results='asis'}

model_index <-
  polpart_index ~
  contact * disc2 +
    ChildWelfare + FamilyCourt + # contact
    Housing + Halfway + # contact
    Jail + Bail + # contact
    Probation + Courts + Police + # contact
    external_efficacy + pol_interest + worship_att + # key control
    partyid + female + age18_29 + age30_39 + age40_64 + # control
    education + inc_less40k + inc_40_79k + race # controls


mod_plots_index_nolink <-
  df_tidy_full %>%
  filter(
    pan_id == 1, c(race == "asian" | race == "latino"),
    type %in% c(
      "Police", "Courts", "Probation",
      "Bail", "Jail", "Halfway",
      "Housing", "FamilyCourt", "ChildWelfare"
    )
  ) %>%
  split(.$type) %>%
  map(~ lm(model_index, data = .))

mod_plots_index_link <-
  df_tidy_full %>%
  filter(
    pan_id == 4, c(race == "asian" | race == "latino"),
    type %in% c(
      "Police", "Courts", "Probation",
      "Bail", "Jail", "Halfway",
      "Housing", "FamilyCourt", "ChildWelfare"
    )
  ) %>%
  split(.$type) %>%
  map(~ lm(model_index, data = .))


tab_labels <- c(
  "Contact", "Discrimination", "ChildWelfare", "Family Court", "Housing",
  "Halfway", "Jail", "Bail", "Probation",
  "Courts", "Police", "Political Efficacy", "Political Interest",
  "Worship Attendance", "Party ID", "Female",
  "Age 40-64", "Age 30-39", "Age 18-29",
  "Education", "Income < 40K", "Income 40-70K", "Latino",
  "Contact X Disc"
)


stargazer(mod_plots_index_link,
  type = "html",
  dep.var.labels = "Political Participaton",
  column.labels = c(
    "Bail", "Court", "Family",
    "Halfway", "Housing", "Jail", "Police",
    "Probation", "ChildWelfare"
  ),
  covariate.labels = tab_labels,
  title = "Moderation analysis: The impact of discrimination and institutional contact on participation, among Asians and Latinos with a strong panethnic identity",
  out = "publication_tables2/mod_int_index_panethnic.tex",
  label = "tab:mod_int_index_panethnic",
  no.space = TRUE,
  font.size = "scriptsize",
  column.sep.width = "2pt",
  omit.stat = c("ser", "f")
)

stargazer(mod_plots_index_nolink,
  type = "html",
  dep.var.labels = "Political Participaton",
  column.labels = c(
    "Bail", "Court", "Family",
    "Halfway", "Housing", "Jail", "Police",
    "Probation", "Welafare"
  ),
  covariate.labels = tab_labels,
  title = "Moderation analysis: The impact of discrimination and institutional contact on participation, among Asians and Latinos without a strong panethnic identity",
  out = "publication_tables2/mod_int_index_nopanethnic.tex",
  label = "tab:mod_int_index_nopanethnic",
  no.space = TRUE,
  font.size = "scriptsize",
  column.sep.width = "2pt",
  omit.stat = c("ser", "f")
)
```
